<?php
declare (strict_types = 1);

namespace app\shop\controller;

use think\annotation\Route;
use think\annotation\route\Group;
use think\annotation\route\Middleware;
use app\common\service\UserService\UserManageService;
use app\BaseShopController;
use think\Request;

/**
 * @Group("Manager")
 * @Middleware({shopMiddileware::class})
 */
class Manager extends BaseShopController
{
    /**
     * 用户登录
     * @Route("Login",method="POST")
     * @return string
     */
    public function userLogin(Request $request )
    {
        $result = callApi(UserManageService::class, 'userLogin',$this->_data);
        return apiResponse($result);
    }
    
    /**
     * 获取用户基本信息
     * @Route("ManagerInfo",method="GET")
     * @return string
     */
    public function ManagerInfo()
    {
        
        $result = callApi(UserManageService::class, 'getSystemLoginUserInfoByUserID', (int)$this->_master_user['manager_id']);
        return apiResponse($result);
    }
    
    /**
     * 用户注册
     * @Route("Register",method="POST")
     * @return string
     */
    public function userRegister(Request $request )
    {
        $result = callApi(UserManageService::class, 'userRegister',$this->_data);       
        return apiResponse($result);
    }
    
    /**
     * 忘记密码
     * @Route("ForgetPassWord",method="POST")
     * @return string
     */
    public function userForgetPassWord(Request $request )
    {
        $result = callApi(UserManageService::class, 'userForgetPassWord',$this->_data);
        return apiResponse($result);
    }
    /**
     * 登出
     * @Route("Logout",method="POST")
     * @return string
     */
    public function userLogout()
    {
        $result = callApi(UserManageService::class, 'userLogout',$this->_all_data);
        return apiResponse($result);
    }
    
    /**
     * 管理员列表
     * @Route("ManagerList",method="GET")
     * @return string
     */
    public function ManagerList()
    {
        $result = callApi(UserManageService::class, 'getManagerList',$this->_all_data);
        return apiResponse($result);
    }
    
    /**
     * 管理员列表分页
     * @Route("ManagerListByPage",method="GET")
     * @return string
     */
    public function ManagerListByPage()
    {
        $result = callApi(UserManageService::class, 'getManagerListByPage',$this->_all_data);
        return apiResponse($result);
    }
    
    /**
     * 新增管理员
     * @Route("addManager",method="POST")
     * @return string
     */
    public function addManager()
    {
        $param= $this->_data;
        $param['cid']= $this->_master_user['cid'];
        $param['cb_id']= $this->_master_user['cb_id'];
        $result = callApi(UserManageService::class, 'addManager',$param);
        if($result['code']==\app\Normal::SUC){
            $this->log(['content'=>'新增管理员【'.$param['name'].'】信息','request_param'=> json_encode($param,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)]);
        }
        return apiResponse($result);
    }
    /**
     * 根据id获取管理员信息
     * @Route("getManagerById/:id",method="GET")
     * @return string
     */
    public function getManagerById($id)
    {
        $param=[
            'id'=>$id,
            'cid'=> $this->_master_user['cid'],
            'cb_id'=> $this->_master_user['cb_id']
        ];
        $result = callApi(UserManageService::class, 'getManagerById',$param);
        unset($result['data']['password']);
        return apiResponse($result);
    }
    
    /**
     * 修改管理员
     * @Route("editManager",method="PUT")
     * @return string
     */
    public function editManager()
    {
        
        $result = callApi(UserManageService::class, 'editManager',$this->_data);
        if($result['code']==\app\Normal::SUC){
            $this->log(['content'=>'修改管理员id为【'.$this->_data['id'].'】的信息', 'request_param'=>json_encode($this->_data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)]);
        }
        return apiResponse($result);
    }
    /**
     * 修改密码
     * @Route("editPass",method="PUT")
     * @return string
     */
    public function editPass()
    {
        
        $result = callApi(UserManageService::class, 'editPass',$this->_all_data);
        if($result['code']==\app\Normal::SUC){
            $this->log(['content'=>'修改登录密码']);
        }
        return apiResponse($result);
    }
    
     /**
     * 管理员日志
     * @Route("getManagerLogListByPage",method="GET")
     * @return string
     */
    public function getManagerLogListByPage()
    {
        $result = callApi(UserManageService::class, 'getManagerLogListByPage',$this->_all_data);
        return apiResponse($result);
    }
    
    /**
     * 管理员权限组(分页)
     * @Route("getAuthGroupListByPage",method="GET")
     * @return string
     */
    public function getAuthGroupListByPage()
    {
        $result = callApi(UserManageService::class, 'getAuthGroupListByPage',$this->_all_data);
        return apiResponse($result);
    }
    /**
     * 管理员权限组
     * @Route("getAuthGropList",method="GET")
     * @return string
     */
    public function getAuthGropList()
    {
        $result = callApi(UserManageService::class, 'getAuthGropList',$this->_all_data);
        return apiResponse($result);
    }
    /**
     * 新增管理员权限组
     * @Route("addAuthGroup",method="POST")
     * @return string
     */
    public function addAuthGroup()
    {
        $result = callApi(UserManageService::class, 'addAuthGroup',$this->_all_data);
        if($result['code']==\app\Normal::SUC){
            $this->log(['content'=>'新增权限组【'.$this->_all_data['name'].'】的信息', 'request_param'=>json_encode($this->_all_data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)]);
        }
        return apiResponse($result);
    }
    
    /**
     * 修改管理员权限组
     * @Route("editAuthGroup",method="PUT")
     * @return string
     */
    public function editAuthGroup()
    {
        $result = callApi(UserManageService::class, 'editAuthGroup',$this->_all_data);
        if($result['code']==\app\Normal::SUC){
            $this->log(['content'=>'修改权限组id【'.$this->_all_data['id'].'】的信息', 'request_param'=>json_encode($this->_all_data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)]);
        }
        return apiResponse($result);
    }
    
    /**
     * 根据id获取权限组信息
     * @Route("getAuthGroupById/:id",method="GET")
     * @return string
     */
    public function getAuthGroupById($id)
    {
        $param=[
            'id'=>$id,
            'cid'=> $this->_master_user['cid'],
            'cb_id'=> $this->_master_user['cb_id']
        ];
        $result = callApi(UserManageService::class, 'getAuthGroupById',$param);
        return apiResponse($result);
    }
     /**
     * 获取权限节点,树行
     * @Route("getAuthNodeList",method="GET")
     * @return string
     */
    public function getAuthNodeList()
    {
        
        $result = callApi(UserManageService::class, 'getAuthNodeList', $this->_master_user);
        return apiResponse($result);
    }
}